package cn.itcast.Rainbow_self_mention.Activity;

import static java.lang.Thread.sleep;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.itcast.Rainbow_self_mention.R;
import cn.itcast.Rainbow_self_mention.Server.CountServer;

public class Login extends AppCompatActivity {

    CountServer.sender sender;
    MyServiceConn conn;
    EditText login_account,login_password;
    Button login,regist;
    Intent server,intent;
    static String sentresult;
    static Boolean jump;
    static Boolean link;
    static Boolean resist;
    TextView reset;


    class MyServiceConn implements ServiceConnection { //用于实现连接服务
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            sender = (CountServer.sender) service;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        reset = findViewById(R.id.forgetpassword);
        server = new Intent(this, CountServer.class);//创建意图对象
        conn = new MyServiceConn();//创建服务连接对象
        bindService(server, conn, BIND_AUTO_CREATE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        init();
        initbutton();
    }
    protected void onDestroy() {
        unbindService(conn);
        super.onDestroy();
    }
    public void init() {
        login_account=findViewById(R.id.login_account);
        login_password=findViewById(R.id.login_password);
        login=findViewById(R.id.login_login);
        regist=findViewById(R.id.login_register);
        reset=findViewById(R.id.forgetpassword);
        jump=false;
        link=false;
        resist=false;

    }
    public void threadwait(int time) {
        try {
            Thread.sleep(time * 1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    class Swatch extends Thread {
        @Override
        public void run() {
            threadwait(1);

            if(link)
            {
                if(jump)
                {
                    Looper.prepare();
                    Transaction t=new Transaction();
                    t.start();
                    Looper.loop();
                }
                else
                {
                    if(resist)
                    {
                        Looper.prepare();
                        Toast.makeText(Login.this,"账号已在别处登录", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }
                    else
                    {
                        Looper.prepare();
                        Toast.makeText(Login.this,"登陆失败", Toast.LENGTH_SHORT).show();
                        Looper.loop();
                    }

                }
            }

            else {
                Looper.prepare();
                Toast.makeText(Login.this,"网络连接失败\n请检查网络", Toast.LENGTH_SHORT).show();
                sender.relink();
                Looper.loop();
            }

        }
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    class Transaction extends Thread {
        @Override
        public void run() {
            super.run();
            Intent intent = new Intent(Login.this, Home.class);
            startActivity(intent);
            overridePendingTransition(0, 0);
        }
    }
    int checkstring(String s){
        String str="abcdefghijklmnopqrstuvwxyz1234567890~`!@#$%^&*()+=*/,.<>?|{}[]-_";
        char c1;
        for(int i=0;i<s.length();i++){
            c1=s.toLowerCase().charAt(i);
            if(str.indexOf(c1)<0){
                return 1;
            }
        }
        return 0;
    }
    int check(String s){
        if(s.equals(""))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    public void initbutton() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account=login_account.getText().toString();
                String password=login_password.getText().toString();
                if(check(account)+check(password)==0)
                {
                    if(checkstring(account)==0)
                    {
                        if(checkstring(password)==0)
                        {
                            sender.Login(account,password);
                            Swatch swatch=new Swatch();
                            swatch.start();
                        }
                        else{
                            Toast.makeText(Login.this, "密码存在非法字符", Toast.LENGTH_SHORT).show();
                        }

                    }
                    else{
                        Toast.makeText(Login.this, "账号存在非法字符", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(Login.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
                }


            }
        });
        regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            intent=new Intent(Login.this, Regist.class);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(Login.this, ResetPassword.class);
                startActivity(intent);
                overridePendingTransition(0, 0);
            }
        });
    }
    public static Handler handler = new Handler()
    {
        public void handleMessage(Message msg)
        {
            Bundle bundle = msg.getData();
            sentresult=bundle.getString("sentresult");
           // if(ed!=null){ed.append('\n' + sentresult);}
            if(sentresult.equals("网络连接错误"))
            {
                link=false;
            }
            if(sentresult.equals("网络连接成功"))
            {
                link=true;
            }
            if(sentresult.equals("登录成功"))
            {
                jump=true;
            }
            if(sentresult.equals("登录失败"))
            {
                jump=false;
            }
            if(sentresult.equals("已在别处登录"))
            {
                jump=false;
                resist=true;
            }
        }

    };





}